import React, {FC, useCallback, useEffect, useMemo, useRef} from "react";
import ListPage from "../../common/ListPage.tsx";
import {Button, Input} from "antd";
import {closeModal, updataModalFooter} from "../../../services/ModalService.tsx";
import {useTranslation} from "react-i18next";
import {showMessage} from "../../../reducer/MsgReducer.tsx";
import {AxiosPost} from "@core/services/HttpService";
import {treeMap} from "../../../utils";

interface AddMaintenanceProps {
    tableProps: any,
    modalKey: string
}

const AddMaintenance: FC<AddMaintenanceProps> = (props) => {

    const listRef = useRef();
    const {t} = useTranslation();

    const onOk = useCallback(async () => {
        const selectData = listRef.current?.cTable()?.getTableSelectedRows() || [];
        if (selectData.length < 1) {
            showMessage({
                type: "error",
                msg: t("pSelectLeastOne")
            });
            return
        }
        const [params = {}] = selectData;
        if (!params.projectCode) {
            showMessage({
                type: "error",
                msg: t("projectCode") + " " + t("notNullValue")
            })
            return
        }
        const result = await AxiosPost("/sf-drs/doc/repairUnit/create", {
            ...params
        });
        result.code == "1" && await closeModal(props.modalKey, async () => {
            return result;
        });
    }, []);

    useEffect(() => {
        const btns = [
            <Button type="primary" onClick={onOk}>{t("determine")}</Button>,
            <Button key="cancel"
                    onClick={() => {
                        closeModal(props.modalKey, false)
                    }}>{t("cancel")}</Button>]
        updataModalFooter(props.modalKey, btns)
    }, []);

    const textEdit = useCallback(({value, node, colDef}) => {
        return (<div>
            <Input size="small" onChange={(e) => {
                node.updateData({...node.data, projectCode: e.target.value})
            }}/>
        </div>)
    }, []);

    const otherRenderFn = useCallback(() => {
        return {
            textEdit: textEdit
        }
    }, []);

    const renderAddMaintenance = useMemo(() => {
        const {tableProps = {}} = props
        return (
            <div className="sf_core_add_maintenance" style={{height: '70vh'}}>
                <ListPage
                    {...tableProps}
                    init={listRef}
                    isInitSearch={false}
                    rowSelection="single"
                    menuId=""
                    otherRenderFn={otherRenderFn}
                    rowKey="virtualId"
                    dataSourceFilter={(data) => {
                        return treeMap(data, (children = [], item) => {
                            return {
                                ...item,
                                children: children.length ? children : undefined,
                                virtualId: Math.floor(Math.random() * 1000000000) + ""
                            }
                        })
                    }}
                />
            </div>
        )
    }, [props]);

    return (renderAddMaintenance);
};

export default React.memo(AddMaintenance);